package ca.tatham.triplipse.views.table;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import ca.tatham.triplipse.RdfStatement;

public class RdfStatementSorter extends ViewerSorter {

	final static int DIRECTION_ASCENDING = 1;

	final static int DIRECTION_DESCENDING = -1;
 
	final static int SUBJECT = 0;

	final static int PREDICATE = 1;

	final static int OBJECT = 2;

	private final static int[] DEFAULT_PRIORITIES = { SUBJECT, PREDICATE,
			OBJECT };

	private final static int[] DEFAULT_DIRECTIONS = { DIRECTION_ASCENDING,
			DIRECTION_ASCENDING, DIRECTION_ASCENDING };

	public static final String SortDirectionAscending_text = "Ascending";

	public static final String SortDirectionDescending_text = "Descending";

	private int[] directions;

	private int[] priorities;

	public RdfStatementSorter() {
		super();
		resetState();
	}

	public int compare(@SuppressWarnings("unused")
	Viewer viewer, Object e1, Object e2) {
		RdfStatement triple1 = (RdfStatement) e1;
		RdfStatement triple2 = (RdfStatement) e2;

		return compare(triple1, triple2, 0);
	}

	public void setTopPriorityDirection(int direction) {
		if (direction == DIRECTION_ASCENDING
				|| direction == DIRECTION_DESCENDING) {
			directions[priorities[0]] = direction;
		}
	}

	public int getTopPriorityDirection() {
		return directions[priorities[0]];
	}

	public int getTopPriority() {
		return priorities[0];
	}

	public void reverseTopPriority() {
		directions[priorities[0]] *= -1;
	}

	public void setTopPriority(int priority) {
		if (priority < 0 || priority >= priorities.length) {
			return;
		}

		int index = -1;
		for (int i = 0; i < priorities.length; i++) {
			if (priorities[i] == priority) {
				index = i;
			}
		}

		if (index == -1) {
			resetState();
			return;
		}

		// shift the array
		for (int i = index; i > 0; i--) {
			priorities[i] = priorities[i - 1];
		}
		priorities[0] = priority;
		directions[priority] = DEFAULT_DIRECTIONS[priority];
	}

	public void resetState() {
		priorities = new int[DEFAULT_PRIORITIES.length];
		System.arraycopy(DEFAULT_PRIORITIES, 0, priorities, 0,
				priorities.length);
		directions = new int[DEFAULT_DIRECTIONS.length];
		System.arraycopy(DEFAULT_DIRECTIONS, 0, directions, 0,
				directions.length);
	}

	public void saveState(IDialogSettings settings) {
		if (settings == null) {
			return;
		}

		for (int i = 0; i < priorities.length; i++) {
			settings.put("priority" + i, priorities[i]);//$NON-NLS-1$
			settings.put("direction" + i, directions[i]);//$NON-NLS-1$
		}
	}

	private int compare(RdfStatement triple1, RdfStatement triple2, int depth) {
		if (depth >= priorities.length) {
			return 0;
		}
		int column = priorities[depth];
		String string1;
		String string2;
		switch (column) {
		case SUBJECT: {
			string1 = triple1.getSubject().toString();
			string2 = triple2.getSubject().toString();
			break;
		}
		case PREDICATE: {
			string1 = triple1.getPredicate().toString();
			string2 = triple2.getPredicate().toString();
			break;
		}
		case OBJECT: {
			string1 = triple1.getObject().toString();
			string2 = triple2.getObject().toString();
			break;
		}
		default:
			throw new IllegalArgumentException("Depth: " + depth);
		}
		int result = collator.compare(string1, string2);
		if (result == 0 && result < priorities.length) {
			return compare(triple1, triple2, depth + 1);
		}
		return result * directions[column];
	}

}
